package com.kanjj.mywebrtc;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.kanjj.mywebrtc.adapter.ChatItemAdapter;
import com.kanjj.mywebrtc.fragment.ChatFragment;
import com.kanjj.mywebrtc.fragment.VideoFragment;
import com.kanjj.mywebrtc.network.SocketClient;

import org.apache.commons.lang3.StringUtils;
import org.webrtc.AudioSource;
import org.webrtc.AudioTrack;
import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
import org.webrtc.CameraEnumerator;
import org.webrtc.Logging;
import org.webrtc.MediaConstraints;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChatActivity extends FragmentActivity {

    private String roomNum;
    private String userName;
    private String addrHost;

    private final String TAG = "ChatActivity";

    @BindView(R.id.chat_content_wrapper)
    LinearLayout wrapperLayout;

    private ChatFragment chatFragment;
    private VideoFragment videoFragment;

    private Fragment currentFragment;

    public static final String CHAT_FRAGMENT_TAG = "chatFragment";
    public static final String VIDEO_FRAGMENT_TAG = "videoFragment";

    private SocketClient socketClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        // 初始化
        Bundle extras = getIntent().getExtras();
        addrHost = (String) extras.get("ServerAddr");
        roomNum = (String) extras.get("RoomName");
        userName = (String) extras.get("userName");
        // init socket
        initSocket();
        // 初始化fragment
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        chatFragment = new ChatFragment();
        videoFragment = new VideoFragment();
        currentFragment = chatFragment;
        fragmentTransaction.add(R.id.chat_content_wrapper, chatFragment, CHAT_FRAGMENT_TAG);
        fragmentTransaction.add(R.id.chat_content_wrapper, videoFragment, VIDEO_FRAGMENT_TAG);
        fragmentTransaction.commit();
    }


    public void initSocket() {
        // 创建 socketClient 对象
        socketClient = SocketClient.newInstance(this, addrHost, roomNum, userName);
    }

    public void gotoFragment(String tag) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (CHAT_FRAGMENT_TAG.equals(tag)) {
            fragmentTransaction.hide(videoFragment).show(chatFragment);
            currentFragment = chatFragment;
        } else {
            fragmentTransaction.hide(chatFragment).show(videoFragment);
            currentFragment = videoFragment;
        }
        fragmentTransaction.commit();
    }


    @Override
    public void onBackPressed() {
        //取出我们返回栈存在Fragment的个数
        if (currentFragment instanceof VideoFragment) {
            gotoFragment(CHAT_FRAGMENT_TAG);
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(videoFragment != null) {
            videoFragment.destroyView();
        }
        socketClient.leaveRoom();
    }


}